// 04jyhs/authAdmin/authAdmin.js
var co = require('../../resource/js/common.js')
var app = getApp()
var _this, _data, _qrParam
Page({
   data: {
      waiter: '',
      waiterList: null,
      i: '',
      printerList: [],
      mode: '',
      edit_id: -1,
      inviteQRCode: '', //二维码图片
   },
   //进入页面执行
   onLoad: function(options) {
      _this = this
      _data = this.data
      console.log(app.Data)
      _this.createQRCode()
      _this.handleWaiterData()
   },
   //修改服务员信息
   submitWaiterInfo: function(e) {
      var param = e.detail.value //获取form表单内容
      param.Biz_ID = app.Data.shopInfo.Biz_ID
      param.User_ID = app.Data.userInfo.User_ID
      param.unionid = app.Data.userInfo.unionid
      param.Waiter_ID = _data.waiterList[e.currentTarget.id].Waiter_ID
      co.getShareApi('waiter/Wmg/save', param, function(res) {
         if (res.code == 0) {
            //添加操作记录
            co.addUseCount('Biz_modify_Waiter')
            _this.setData({
               edit_id: -1
            })
            _this.onLoad()
         }
      })
   },
   //编辑服务员
   editwaiter: function(e) {
      if (app.Data.shopInfo.User_Identity == 'boss' || app.Data.shopInfo.User_Identity == 'worker') {
         var index = e.currentTarget.id
         this.setData({
            edit_id: _data.edit_id == index ? -1 : index
         })
      } else {
         co.toastAllText('需要掌柜登录才能操作')
      }
   },
   //删除服务员
   delConfirm: function(e) {
      if (app.Data.shopInfo.User_Identity == 'boss' || app.Data.shopInfo.User_Identity == 'worker') {
         co.confirm('', '确认删除吗？', '取消', '删除', function(res) {
            if (res) {
               _this.delWaiter(e)
            }
         })
      } else {
         co.toastAllText('需要掌柜登录才能操作')
      }
   },
   delWaiter: function(e) {
      if (app.Data.shopInfo.User_Identity == 'boss') {
         let i = e.currentTarget.id
         var list = _data.waiterList
         co.getShareApi('waiter/Wmg/delete', {
            Biz_ID: app.Data.shopInfo.Biz_ID,
            Waiter_ID: list[i].Waiter_ID
         }, function(res) {
            if (res.code == 0) {
               //添加操作记录
               co.addUseCount('Biz_delete_Waiter')
               list.splice(i, 1)
               _this.setData({
                  waiterList: list
               })
            }
         })
      } else {
         co.toastAllText('需要掌柜登录才能操作！')
      }
   },

   //获取服务员列表 
   handleWaiterData: function() {
      co.getShareApi('waiter/Wmg/getWaiterList', {
         Biz_ID: app.Data.shopInfo.Biz_ID
      }, function(res) {
         if (res.code == 0) {
            _this.setData({
               waiterList: res.data
            })
         }
      })
   },

   //读取显示小程序码图片
   createQRCode() {
      var param = app.Data.app
      param.scene = "BID." + app.Data.shopInfo.Biz_ID + '.IID.' + app.Data.userInfo.User_ID
      param.wxapp_page_path = '04jyhs/authAdmin/agreeTo/agreeTo'
      var url = "https://h5.jyhs.ls293.cn/" + param.appid + '/' + param.wxapp_page_path + "/" + param.scene + 's.png'
      _this.setData({
         inviteQRCode: url
      })
      _qrParam = param
   },
   onImgError1() {
      _this.setData({
         inviteQRCode: false
      })
   },
   // //小程序二维码图片加载错误时回调
   onImgError: function() {
      co.getJyhsApi('api/Weixin/getWxAppCodeUnlimit', _qrParam, function(res) {
         if (res.code == 0) {
            _this.setData({
               inviteQRCode: res.data.wxapp_code_url
            })
         }
      })
   },
   //分享
   onShareAppMessage: function() {
      var urlParam = ''
      if (app.Data.userInfo.User_ID) {
         urlParam = '?scene=BID.' + app.Data.shopInfo.Biz_ID + '.IID.' + app.Data.userInfo.User_ID
      }
      console.log('app.Data.shopInfo.Biz_ID=====', app.Data.shopInfo.Biz_ID)
      console.log('urlParam===', urlParam)
      console.log('IID=====', app.Data.userInfo.User_ID)
      return {
         title: '',
         path: '/04jyhs/authAdmin/agreeTo/agreeTo' + urlParam,
         success: function(res) {
            console.log("【分享成功res】===", res)
            // var pages = getCurrentPages() //获取加载的页面
            // var currentPage = pages[pages.length - 1] //获取当前页面的对象
            // app.getShareInfo(res, '邀请回收人员', currentPage.route)
         }
      }
   },
})